package com.netty.esc.controller;

import com.alibaba.fastjson.JSONObject;
import com.netty.esc.Interceptor.annotation.ControllerMapping;
import com.netty.esc.Interceptor.annotation.FuckAll;
import com.netty.esc.common.base.BaseResponseDTO;
import com.netty.esc.common.global.UserCache;
import com.netty.esc.entity.model.FriendsOnlineDTO;
import com.netty.esc.entity.model.TUserFriendsDTO;
import com.netty.esc.entity.pojo.UserFriends;
import com.netty.esc.entity.pojo.UserInfo;
import com.netty.esc.service.http.FriendService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: ext.zhaoyang17
 * @date: 2022.10.21
 */
@FuckAll
@ControllerMapping("/friend")
public class FriendController {

    @Resource
    FriendService friendService;


    /**
     * 查询用户好友列表
     */

    @GetMapping("/list")
    public BaseResponseDTO<?> findList(){
        UserInfo currentUser = UserCache.getCurrentUser();
        Long id = currentUser.getId();
        List<TUserFriendsDTO> friendList = friendService.findFriendList(id);
//        List<PersonEntity> all = userRepository.findRelation(id).orElse(new ArrayList<>());
        return BaseResponseDTO.setResultSuccess(friendList);
    }

    /**
     * 查询好友在线状态列表
     * @Param: userList 好友列表
     */
//    @RequiresRoles(value = {"user"})
    @PostMapping("/online")
    public BaseResponseDTO<?> findUserOnline(){
        List<FriendsOnlineDTO> friendOnline = friendService.findFriendOnlines();
        return BaseResponseDTO.setResultSuccess(friendOnline);
    }

    /**
     * 查询好友
     * @param json telephone 查询的好友手机号
     */
    @RequiresRoles(value = {"admin","user"},logical = Logical.OR)
    @PostMapping("/find")
    public BaseResponseDTO<?> findFriend(@RequestBody JSONObject json){
        //按手机号查询
        String telephone = json.getString("telephone");
        UserInfo friend = friendService.findFriend(telephone);
        return friend==null?BaseResponseDTO.setResultSuccess("无此好友信息"):BaseResponseDTO.setResultSuccess(friend);
    }

    /**
     * 添加好友
     */
//    @RequiresRoles(value = {"user"})
    @PostMapping("/add")
    public BaseResponseDTO<?> addFriend(@RequestBody JSONObject json){
        UserInfo currentUser = UserCache.getCurrentUser();
        Long userId = currentUser.getId();
        String telephone = json.getString("telephone");
        String message = json.getString("message");
        try {
            friendService.addFriend(userId,telephone,message);
        }catch (RuntimeException ex){
            return BaseResponseDTO.setResultError(ex.getMessage());
        }

        return BaseResponseDTO.setResultSuccess();
    }

    /**
     * 拉取好友请求列表
     * */
    @RequiresRoles(value = {"user"})
    @GetMapping("/request/list")
    public BaseResponseDTO<?> friendRequestList(){
        List<UserFriends> userInfoDTOList = friendService.friendRequestList();
        return BaseResponseDTO.setResultSuccess(userInfoDTOList);
    }

    /**
     * 同意好友添加申请
     */
    @RequiresRoles(value = {"user"})
    @PostMapping("/access")
    public BaseResponseDTO<?> assentApplyFriend(@RequestBody JSONObject json){
        UserInfo currentUser = UserCache.getCurrentUser();
        Long userId = currentUser.getId();
        String telephone = json.getString("telephone");
        friendService.assentApplyFriend(userId,telephone);
        return BaseResponseDTO.setResultSuccess();
    }

    @RequiresRoles(value = {"user"})
    @PostMapping("/blacklist")
    public BaseResponseDTO<?> pushOneToBlacklist(@RequestBody JSONObject json){
        String telephone = json.getString("telephone");
        friendService.pushOneToBlacklist(telephone);
        return BaseResponseDTO.setResultSuccess();
    }
}
